package com.thewell79.areadiary.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.cengalabs.flatui.views.FlatEditText;
import com.dd.processbutton.iml.ActionProcessButton;
import com.thewell79.areadiary.R;
import com.thewell79.areadiary.activity.MainActivity;
import com.thewell79.areadiary.request.HttpConfig;
import com.thewell79.areadiary.request.PostStringRequest;

/**
 * Created by THEWELL-LOBBY on 2015-04-27.
 */

public class LoginFragment extends Fragment {

    private ActionProcessButton btn_login;

    private EditText email, pw;

    private PostStringRequest request = new PostStringRequest(Request.Method.GET, HttpConfig.USER);

    // 데이터 로드시
    private PostStringRequest.OnReqeust onReqeust = new PostStringRequest.OnReqeust() {


        @Override
        public void onLoading() {
            email.setEnabled(false);
            pw.setEnabled(false);

            btn_login.setProgress(1);
        }

        @Override
        public void onEndRequest(String response) {

        }

        @Override
        public void onEnd(boolean isError, VolleyError error) {
            if (isError) {
                btn_login.setProgress(100);
            } else {
                email.setEnabled(true);
                pw.setEnabled(true);
                btn_login.setProgress(-1);
            }
        }
    };

    // 버튼 클릭시
    private View.OnClickListener btn_click = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);

            getActivity().finish();
        }
    };

    //  뷰 정의
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_login, container, false);

        email = (EditText) v.findViewById(R.id.edit_email);
        pw = (EditText) v.findViewById(R.id.edit_pass);

        btn_login = (ActionProcessButton) v.findViewById(R.id.btn_login);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        btn_login.setMode(ActionProcessButton.Mode.ENDLESS);
        btn_login.setOnClickListener(btn_click);

        request.setOnLoading(onReqeust);

    }
}